package vg.modules.desktop.components;

import java.awt.GridLayout;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.JComponent;
import javax.swing.JPanel;

import vg.services.graph_view_manager.interfaces.tab.ATab;

/**
 * Wrapper for IGraphView, which may be use for TabbedPane.
 * All methods of this class by need called from EDT only!!!
 * @author tzolotuhin
 */
public class TabComponent extends JPanel {
	// Static members
	private static final long serialVersionUID = 1L;
	private static AtomicInteger counterId = new AtomicInteger(0);
	
	// Main components
	private final ATab tab;
	private final int id; // if of tab
	private int number; // if tab pane has many same titles

	/**
	 * Constructor creates wrapper for ATab.
	 * And it may be use for TabbedPane.
	 */
	public TabComponent(ATab tab) {
		super(new GridLayout(1,1));
		this.tab = tab;

		this.number = 0;
		this.id = getNextId();		
		
		if (tab != null) {
			JComponent tmp_view = tab.getView();
			if (tmp_view != null)
				add(tmp_view);
		}
	}
		
	public void vg_setNumber(int newNumber) {
		this.number = newNumber;
	}
	
	public int vg_getNumber() {
		return number;
	}
	
	public int vg_getId() {
		return id;
	}
	public String vg_getTabTitle() {
		return tab == null ? null : tab.getTabTitle();
	}
	
	public String vg_getFullTitle() {
		return number == 0 ? vg_getTabTitle() : vg_getTabTitle() + ":" + number;
	}
	
	public ATab vg_getTab() {
		return tab;
	}
	
	/**
	 * This method returns next id for tab.
	 */
	private int getNextId() {
		int a = counterId.incrementAndGet();
		return a;
	}
}
